﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using Microsoft.BizTalk.Message.Interop;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.XLANGs.BaseTypes;
using BizMock;
using Microsoft.BizTalk.ExplorerOM;

namespace BizMock
{
    

    public class XmlMessageVerifierArtifact : XmlMessageVerifier
    {
    }

    public class FlatMessageVerifierArtifact : FlatMessageVerifier
    {
    }

    public class MapArtifact
    {
        TransformBase _map;
        public MapArtifact(TransformBase map)
        {
            _map = map;
        }

        public TransformBase Map { get { return _map; } }
    }

    public class SendPortArtifact : IDeployPort
    {

        public SendPortArtifact(string portName)
        {
            PortName = portName;
        }

        public SendPortArtifact(string portName, int timeout)
            : this( portName)
        {
            Timeout = timeout;
        }

        public SendPortArtifact(string portName, string sendPipelineFullName, int timeout)
            : this( portName, timeout)
        {
            SendPipelineFullName = sendPipelineFullName;
        }

        public SendPortArtifact(string portName, string sendPipelineFullName, string sendPipelineData, int timeout)
            : this(portName, sendPipelineFullName, timeout)
        {
            SendPipelineData = sendPipelineData;
        }

        public SendPortArtifact(string portName, string sendPipelineFullName, string sendPipelineData, int timeout, bool dynamic )
            : this(portName, sendPipelineFullName, sendPipelineData, timeout)
        {
            Dynamic = dynamic;
        }

        public string PortName;
        public int Timeout = 90;
        public string SendPipelineFullName;
        public string SendPipelineData;
        public bool Dynamic = false;

        public virtual void Deploy(Application application)
        {
            BizTalkExplorerHelper.CreateNewSendPort(application, PortName, SendPipelineFullName, SendPipelineData, null, null, Timeout, false, Dynamic );
        }

        public void Undeploy(Application application)
        {
            BizTalkExplorerHelper.RemoveSendPort(application, PortName);
        }

    }

    public class SolicitResponsePortArtifact : SendPortArtifact
    {
        public SolicitResponsePortArtifact( string portName)
            : base( portName)
        {
        }
        public SolicitResponsePortArtifact(string portName, int timeout)
            : base( portName, timeout)
        {
        }
        public SolicitResponsePortArtifact( string portName, string sendPipelineFullName, string receivePipelineFullName, int timeout)
            : base( portName, sendPipelineFullName, timeout)
        {
            ReceivePipelineFullName = receivePipelineFullName;
        }
        public SolicitResponsePortArtifact(string portName, string sendPipelineFullName, string sendPipelineData, string receivePipelineFullName, string receivePipelineData, int timeout)
            : base(portName, sendPipelineFullName, sendPipelineData, timeout)
        {
            ReceivePipelineFullName = receivePipelineFullName;
            ReceivePipelineData = receivePipelineData;
        }

        public SolicitResponsePortArtifact(string portName, string sendPipelineFullName, string sendPipelineData, string receivePipelineFullName, string receivePipelineData, int timeout, bool dynamic )
            : this( portName, sendPipelineFullName, sendPipelineData, receivePipelineFullName, receivePipelineData, timeout )
        {
            Dynamic = dynamic;
        }


        public string ReceivePipelineFullName;
        public string ReceivePipelineData;

        public override void Deploy(Application application)
        {
            BizTalkExplorerHelper.CreateNewSendPort(application, PortName, SendPipelineFullName, SendPipelineData, ReceivePipelineFullName, ReceivePipelineData, Timeout, true, Dynamic);
        }
    }

    public class ReceiveLocationArtifact : IDeployPort
    {
        string _rclName;
        int _timeout;
        string _receivePipelineFullName;
        string _receivePipelineData;

        public ReceiveLocationArtifact(string rclName, int timeout)
        {
            _rclName = rclName;
            _timeout = timeout;
        }

        public ReceiveLocationArtifact( string rclName, string receivePipelineFullName, int timeout)
            :this(rclName,timeout)
        {
            _receivePipelineFullName = receivePipelineFullName;
        }

        public ReceiveLocationArtifact(string rclName, string receivePipelineFullName, string receivePipelineData, int timeout)
            : this(rclName, timeout)
        {
            _receivePipelineFullName = receivePipelineFullName;
            _receivePipelineData = receivePipelineData;
        }

        public string RclName { get { return _rclName; } }

        public int Timeout { get { return _timeout; } }



        public virtual void Deploy( Application application )
        {
            BizTalkExplorerHelper.CreateNewReceiveLocation( application, RclName, RclName, _receivePipelineFullName, _receivePipelineData, null, null, Timeout, false);
        }

        public void Undeploy( Application application )
        {
            BizTalkExplorerHelper.RemoveReceiveLocation( application,  RclName);
        }

        public void Enable(Application application, bool enable)
        {
            BizTalkExplorerHelper.EnableReceiveLocation( application, _rclName, enable);
        }

    }

    public class RequestResponseLocationArtifact : ReceiveLocationArtifact
    {
        private string _receivePipelineFullName;
        private string _sendPipelineFullName;
        private string _receivePipelineData;
        private string _sendPipelineData;

        public RequestResponseLocationArtifact(string rclName, int timeout)
            : base(rclName, timeout)
        {
        }

        public RequestResponseLocationArtifact(string rclName, string receivePipelineFullName, string sendPipelineFullName, int timeout)
            : base(rclName, timeout)
        {
            _receivePipelineFullName = receivePipelineFullName;
            _sendPipelineFullName = sendPipelineFullName;
        }
        public RequestResponseLocationArtifact(string rclName, string receivePipelineFullName, string receivePipelineData, string sendPipelineFullName, string sendPipelineData, int timeout)
            : base(rclName, timeout)
        {
            _receivePipelineFullName = receivePipelineFullName;
            _sendPipelineFullName = sendPipelineFullName;
            _receivePipelineData = receivePipelineData;
            _sendPipelineData = sendPipelineData;
        }

        public override void Deploy(Application application)
        {
            BizTalkExplorerHelper.CreateNewReceiveLocation( application, RclName, RclName, _receivePipelineFullName, _receivePipelineData, _sendPipelineFullName, _sendPipelineData ,Timeout, true);
        }

    }

    public class DatabaseArtifact
    {
        public DatabaseArtifact(string connectionString)
        {
            ConnectionString = connectionString;
        }

        public string ConnectionString;
    }

    public class EventLogArtifact
    {
        private string _log;
        private string _source;
        private int _count;
        private const int LAST_ENTRIES = 25;
        public EventLogArtifact(string log, string source)
            : this(log, source, LAST_ENTRIES)
        {
        }

        public EventLogArtifact(string log, string source, int count)
        {
            _log = log;
            _source = source;
            _count = count;
        }

        public string Log { get { return _log; } }

        public string Source { get { return _source; } }

        public int Count { get { return _count; } }
    }

    public interface IDeployPort
    {
        void Deploy(Application application);
    }
    
    public class OrchestrationArtifact
    {
        Application application;
        List<IDeployPort> ports;
        public string orchestrationFullName;
        Dictionary<string, string> _bindingMap;

        public OrchestrationArtifact(Application application, string fullName, List<IDeployPort> ports, Dictionary<string,string> bindingMap )
        {
            this.application = application;
            this.ports = ports;
            orchestrationFullName = fullName;
            _bindingMap = bindingMap;
        }
        
        public void Start()
        {
            BizTalkExplorerHelper.BindOrchestration(application, orchestrationFullName, _bindingMap);
            BizTalkExplorerHelper.StartOrchestration(application, orchestrationFullName);
            BizTalkExplorerHelper.EnableReceiveLocations(application);
            BizTalkExplorerHelper.ApplyChanges(application);
        }

        public void Stop()
        {
            BizTalkExplorerHelper.DisableReceiveLocations(application);
            BizTalkExplorerHelper.StopOrchestration(application, orchestrationFullName);
            BizTalkExplorerHelper.UnBindOrchestration(application, orchestrationFullName);
            BizTalkExplorerHelper.ApplyChanges(application);
        }

    }
}
